<?php
// 引入配置库
include_once('./config/config.php');
// 判断是不是post请求
if ($_POST) {
    // 接收参数
    $username = isset($_POST['username']) ? trim($_POST['username']) : '';
    $password = isset($_POST['password']) ? trim($_POST['password']) : '';
    $code = $_POST['code'] ?? '';
    if (empty($username)) {
        notice('用户名不能为空');
    }
    if (empty($password)) {
        notice('密码不能为空');
    }
    if (empty($code)) {
        notice('验证码不能为空');
    }
    $recode = $_SESSION['code'] ?? '';
    // 判断验证码是否输入正确(不区分大小写)
    if (strcasecmp($code, $recode)) {
        notice('验证码错误');
    }
    // 查询admin数据表是否存在这个用户名
    $sql = "SELECT * FROM `pre_admin` WHERE `username` = '$username'";
    $admin = find($sql);
    if (empty($admin)) {
        notice('用户名不存在');
    }
    $repass = md5($password . $admin['salt']);
    if ($repass != $admin['password']) {
        notice('密码错误');
    }
    // 提取登录信息重新组装 
    $data = [
        'id' => $admin['id'],
        'username' => $admin['username'],
        'avatar' => $admin['avatar']
    ];
    // 存储登录信息
    setcookie('auth', json_encode($data));
    notice('登录成功', 'index.php');
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>login</title>
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- 公共样式 -->
    <?php include_once("./common/meta.php"); ?>
    <!-- jquery -->
    <script src="./assets/lib/jquery-1.7.2.min.js" type="text/javascript"></script>
</head>

<body>

    <div class="navbar">
        <div class="navbar-inner">
            <a class="brand" href="index.php"><span class="second">Admin</span></a>
        </div>
    </div>

    <div class="row-fluid">
        <div class="dialog">
            <div class="block">
                <p class="block-heading">登录</p>
                <div class="block-body">
                    <form method="post">
                        <label>用户名</label>
                        <input type="text" class="span12" name="username" id="username">
                        <!-- <span id="username-error"></span> -->
                        <label>密码</label>
                        <input type="password" class="span12" name="password" id="password">
                        <label>验证码</label>
                        <input type="text" class="span8" name="code">
                        <img src="./config/imgcode.php" alt="" onclick="this.src='./config/imgcode.php?id='+Math.random()+''" style="cursor: pointer;">
                        <label></label>
                        <button href="index.php" class="btn btn-primary pull-right">登录</button>
                        <div class="clearfix"></div>
                    </form>
                </div>
            </div>
        </div>
    </div>

</body>

</html>
<!-- 公共js -->
<?php include_once('./common/script.php') ?>
<script>
    /*  $('#username').change(() => {
        // 获取用户输入的username
        const username = $('#username').val() ? $.trim($('#username').val()) : ''
        // 发请求
        $.ajax({
            type: 'post',
            url: 'api.php',
            data: {
                'action': 'select_username',
                username
            },
            dataType: 'json',
            success: res => {
                // console.log(res)
                if (res.code === 1) {
                    $('#username-error').text(res.msg).css({
                        color: 'green',
                        fontSize: '16px'
                    })
                } else {
                    $('#username-error').text(res.msg).css({
                        color: 'red',
                        fontSize: '16px'
                    })
                }
            }
        })
    }) */
</script>